// pages/staff/orderDetail/orderDetail.ts

import {
  sonOrderDetail,
  childOrderOver,
  staffOrderDetail,
  childOrderOverByCode,
  sendPic
} from "../../../http/api"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    goodName: "",
    orderStatusStr: "",
    orderStatusValue:"",
    code: "",
    imgArr:[],
    isShow:false,
    imgIndex:0
  },

  handleShow(e){
    const {index} = e.currentTarget.dataset
    this.setData({
      isShow:true,
      imgIndex:index
    })
  },

  handleClose(){
   
    this.setData({
      isShow:false
    })
  },

  handlePhotograph(){
    const that = this

    wx.chooseMedia({
      count: 9,
      mediaType: ['image'],
      sourceType: ['camera'],
      maxDuration: 30,
      camera: 'back',
      sizeType:['original'],
      success(res) {
        
        wx.showLoading({
          title:"上传中"
        })
      
        
        wx.uploadFile({
          filePath: res.tempFiles[0].tempFilePath,
          name: 'file',
          url: app.globalData.baseUrl+'/wechat/orderchild/send-pic',
          header: {
            "Content-Type": "multipart/form-data"
          },
          formData: {
            orderId:that.data.orderId,
            id:that.data.id,
            file: res.tempFiles[0].tempFilePath,
          },
          success:function(res){
            const decodedData = JSON.parse(res.data)
            if(decodedData.code===200 ){
              that.setData({
                imgArr:[...that.data.imgArr,decodedData.data]
              })
            }
          },
          fail:function(res){
            
            wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
              })
          },
          complete(){
            wx.hideLoading()
          }

        })
       
      }
    })
  },

  handleScan() {
    const that = this
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ['qrCode'],
      fail(res) {
        console.log(res)
      },
      success(res) {
        if (res.errMsg === 'scanCode:ok') {
          that.setData({
            code: res.result
          })
          that.getScanRes();
        } else {
          wx.showToast({
            title: '扫码失败',
            icon: "error"
          })
        }

      }

    })


  },

  getScanRes() {
    wx.showLoading()
    const params = {
      id: this.data.id,
      orderId: this.data.orderId,
      status: 2,
      personCode: this.data.code
    }
    childOrderOverByCode(params).then(res => {
      if (res.data.code === 200) {
        wx.showModal({
          title: '提示',
          content: '已成功核销',
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
       
      }else{
        wx.showModal({
          title: '提示',
          content: res.data.msg,
          showCancel:false,
         
        })
      }
    }).finally(() => {
      wx.hideLoading()
    })
  

  },

  handleConfirm() {

    wx.showLoading()
    const params = {
      id: this.data.id,
      orderId: this.data.orderId,
      status: 1
    }
    childOrderOver(params).then(res => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '提交成功',
          icon: 'success'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1000)
      }

    }).finally(() => {
      wx.hideLoading()
    })
  },

  getData() {

    const {
      id,
      orderType,
      orderId
    } = this.data
    const params = {
      id,
      orderType,
      orderId
    }


    staffOrderDetail(params).then(res => {
      if (res.data.code === 200) {
        this.setData({
          orderObj: {
            ...res.data.data,
          },
          
        })

      

        

        const orderStatusArr = app.getSonOrderStatusArr()
        let orderStatusStr = ""
        let orderStatusValue = ""


        //外卖单
        if(orderType==0){
          orderStatusStr = orderStatusArr[this.data.orderObj.status]
          orderStatusValue = this.data.orderObj.status
        }
        //包月单
        if(orderType==1){
          orderStatusStr = orderStatusArr[this.data.orderObj.orderChildList[0].status]
          orderStatusValue = this.data.orderObj.orderChildList[0].status

          if(res.data.data.orderChildList[0].sendPicList){
            this.setData({
              imgArr:[...res.data.data.orderChildList[0].sendPicList]
            })
          }
        }

        this.setData({
          orderStatusStr,
          orderStatusValue
        })
        
       

      }
    }).finally(() => {
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {
      id,
      orderType,
      orderId
    } = options
    if (id && orderType && orderId) {
      this.setData({
        id,
        orderType,
        orderId
      })
      this.getData()
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.setData({
    //   orderStatus: app.getSonOrderStatusArr()
    // })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})